#coding=utf-8
import os,psutil
import time
from selenium.common.exceptions import NoSuchElementException

import huawei_market_appium as HuaWei
import samsung_market_appium as SamSung
import KuAn_market_appium as Coolapk
import Nduoba_market_appium as Nduo
import yinyonghui_market_appium as YingYongHui
import mumayi_market_appium as MuMaYi
import anzhi_market_appium as AnZhi
import wandoujia_market_appium as WanDouJia
import PPzhushou_market_appium as PPzhuShou
import yingyongbao_market_appium as YingYongBao


import dangle_market_appium as DangLe
import jifeng_market_appium as JiFeng
import kupai_market_appium as KuPai
import sougou_market_appium as SouGou
import youyi_market_appium as YouYi


import qihoo_market_appium as QiHoo360
import baidu_market_appium as BaiDu
import lenovo_market_appium as Lenovo
import jinli_market_appium as JinLi
import liqu_market_appium as LiQu


dev_list={}
#dev_list['MuMaYi']=MuMaYi
#dev_list['HuaWei']=HuaWei
#dev_list['YingYongHui']=YingYongHui
#dev_list['Nduo']=Nduo
#dev_list['AnZhi']=AnZhi
#dev_list['WanDouJia']=WanDouJia
#dev_list['PPzhuShou']=PPzhuShou
#dev_list['YingYongBao']=YingYongBao
#dev_list['SamSung']=SamSung

#dev_list['DangLe']=DangLe
#dev_list['JiFeng']=JiFeng
#dev_list['KuPai']=KuPai
#dev_list['SouGou']=SouGou
#dev_list['YouYi']=YouYi

#dev_list['QiHoo360']=QiHoo360
#dev_list['BaiDu']=BaiDu
#dev_list['Lenovo']=Lenovo
#dev_list['JinLi']=JinLi
dev_list['LiQu']=LiQu


def get_pid_by_name(name, pid_list):
	plist = psutil.process_iter()
	print name
	for temp in plist:
		#print ', '.join(['%s:%s' % item for item in temp.__dict__.items()])
		if temp.name() == name:
			pid_list.append(temp.pid)

def killCapture():
	#os.system('taskkill /f /im adb.exe')
	adb_del_list = []
	get_pid_by_name('test_http_packet.exe', adb_del_list)
	print 'test_http_packet.exe del: ' + str(adb_del_list)
	for pid_get in adb_del_list:
		os.system('taskkill /f  /t /PID ' + str(pid_get))
	return True

def start_capture(dev_name):
    print "stop capture pid"
    killCapture()
    print "restart capture pid"
    os.system('start test_http_packet.exe '+dev_name)

def stop_capture():
    killCapture()
if __name__ == '__main__':
    while True:
        for dev_key in dev_list:
            print "start test "+dev_key
            start_capture(dev_key)
            print "start Appium"
            dev_list[dev_key].run()
            print "kill capture pid"
            killCapture()
            time.sleep(10)
